<?php

	/**
	 * TV shows class, basic searching functionality
	 * 
	 * @package PHP::TVDB
	 * @author Ryan Doherty <ryan@ryandoherty.com>
	 */

	class TV_Shows extends TVDB {

		/**
		 * Searches for tv shows based on show name
		 * 
		 * @var string $showName the show name to search for
		 * @access public 
		 * @return array An array of TV_Show objects matching the show name
		 **/
		public static function search($showName) {
			$params = array('action' => 'search_tv_shows', 'show_name' => $showName);
			$data = self::request($params);
			
			if($data) {
				$xml = simplexml_load_string($data);
				$shows = array();
				foreach($xml->Series as $show) {
					$shows[] = self::findById((string)$show->seriesid, TRUE);
				}
		
				return $shows;
			}
		}
		
		/**
		 * Find a tv show by the id from thetvdb.com
		 *
		 * @return TV_Show|false A TV_Show object or false if not found
		 **/
		public static function findById($showId, $search=false, $all=FALSE) {
			$params = array('action' => 'show_by_id', 'id' => $showId, 'all' => $all);
			$data = self::request($params);
		
			if ($data) {
				$xml = simplexml_load_string($data);		
				
				if($all){
					$json_list_series	= json_encode($xml);
					$data	= json_decode($json_list_series,true);
					return $data;
				}
				
				$show = new TV_Show($xml->Series);
				
				//Si es false obtenemos la informacion en español
				if(!$search){
					$params['lang'] = 'es';
					$data = self::request($params);
					
					if ($data) {
						$xml = simplexml_load_string($data);
						$show_spa = new TV_Show($xml->Series);
						$show->overview_spa = $show_spa->overview;
					}
				}
				
				return $show;
			} else {
				return false;
			}
		}
	}

?>